package 单周赛.september;

import java.util.HashMap;
import java.util.Map;

/**
 * @
 * @date 2024/05/05
 */
public class 第396场单周赛 {

    public static void main(String[] args) {

        System.out.println(isValid("AhI"));

        System.out.println(minimumOperationsToMakeKPeriodic("leetcodeleet", 4));
    }

    public static boolean isValid(String word) {

        if (word.length() < 3) {
            return false;
        }
        boolean yuan = false, fu = false;
        for (char c : word.toCharArray()) {

            if (!Character.isLetterOrDigit(c)) {
                return false;
            }
            if (Character.isDigit(c)) {
                continue;
            }
            if (((c | 32) == 'a') || ((c | 32) == 'e') || ((c | 32) == 'i') || ((c | 32) == 'o') ||
                    (c | 32) == 'u') {
                yuan = true;
            } else {
                fu = true;
            }
        }
        return yuan && fu;
    }

    /**
     * 哈希表+贪心
     */
    public static int minimumOperationsToMakeKPeriodic(String word, int k) {
        int max = 0;
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < word.length(); i += k) {

            String substring = word.substring(i, i + k);
            map.put(substring, map.getOrDefault(substring, 0) + 1);

            max = Math.max(max, map.get(substring));
        }

        return (word.length() - max * k) / k;
    }

    /**
     * 模拟
     */
    public int minAnagramLength(String s) {
        if (s.length() == 1) {
            return 1;
        }
        int min = 1;
        while (true) {
            if (s.length() % min != 0) {
                min++;
                continue;
            }
            if (min == s.length()) {
                return s.length();
            }

            int[] preSum = new int[26];
            for (int i = 0; i < min; i++) {
                preSum[s.charAt(i) - 'a']++;
            }


            boolean flag = true;
            for (int i = min; i < s.length(); i += min) {

                int[] curSum = new int[26];
                for (int j = i; j < i + min; j++) {
                    curSum[s.charAt(j) - 'a']++;
                }
                if (!compare(preSum, curSum)) {
                    flag = false;
                    break;
                }
            }

            if (flag) {
                return min;
            }
            min++;
        }
    }

    private boolean compare(int[] preSum, int[] curSum) {
        for (int n = 0; n < 26; n++) {
            if (preSum[n] != curSum[n]) {
                return false;
            }
        }
        return true;
    }
}
